package com.zou.controller.proscenium;

import com.zou.controller.proscenium.common.InitCommon;
import com.zou.pojo.Info;
import com.zou.pojo.MaxMenu;
import com.zou.pojo.MinMenu;
import com.zou.service.InfoService;
import com.zou.service.MaxMenuService;
import com.zou.service.MinMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class AllnewsController {
    @Autowired
    private InfoService infoService;
    @Autowired
    private MaxMenuService maxMenuService;
    @Autowired
    private MinMenuService minMenuService;

    @GetMapping("/proscenium/allnews/{menu}/{id}")
    public String allnews(@PathVariable String menu, @PathVariable int id, Model model) {
        model = new InitCommon(infoService, maxMenuService, minMenuService, model).initHtml();

        Info info = new Info();
        String title = "";
        try {
            if (menu.equals("min")) {
                info.setMinId(id);
                title = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMinId(id)).get(0).getMinName();
            } else if (menu.equals("max")) {
                info.setMaxId(id);
                title = maxMenuService.querySomeMaxMenuByMaxMenu(new MaxMenu().setMaxId(id)).get(0).getMaxName();
            } else {
                return "forward:index";
            }
        } catch (Exception e) {
            return "forward:index";
        }
        model.addAttribute("title", title);
        List<Info> infos = infoService.querySomeInfoByInfo(info);
        model.addAttribute("infos", infos);

        return "proscenium/allnews";
    }
}
